package com.bxd.baodan.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class QuickIndexBar extends View {
    private String[] indexArr = {"热","A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z"};
    private Paint paint;
    private int width;
    private float cellHeight;
    public OnTouchLetterListener listener;

    public void setOnTouchLetterListener(OnTouchLetterListener listener) {
        this.listener = listener;
    }

    public QuickIndexBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public QuickIndexBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public QuickIndexBar(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        paint.setColor(Color.WHITE);
        paint.setTextSize(30);//默认sp
        paint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        cellHeight = getMeasuredHeight() * 1f / indexArr.length; //todo  得到一个格子的高度,乘以1f精度高,高度均匀
    }

    /**
     * todo 绘制用onDraw,onDraw用canvas
     * <p>
     * fixme 绘制文字居中时,文字的起点是控件宽度的一半 x = width/2
     * <p>
     * fixme 绘制文本y坐标(底部) 格子高度的一半 + 文本高度的一半 + position*格子高度
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawPicture(new Picture());todo 用于验证码
        for (int i = 0; i < indexArr.length; i++) {
            float x = width / 2;


            int textHeight = getDrawTextHeight(indexArr[i], paint);
            //todo  绘制文本y坐标(底部) = 格子高度的一半 + 文本高度的一半 + position*格子高度

            float y = cellHeight / 2 + textHeight / 2 + i * cellHeight;

            //todo 表明当前绘制的就是同一个,需要变色
            paint.setColor(lastIndex == i ? Color.RED : Color.BLACK);

            canvas.drawText(indexArr[i], x, y, paint);//文字起点默认在文字的左下角
        }
    }

    private int getDrawTextHeight(String text, Paint paint) {
        //todo 获取绘制文本的高度
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    /**
     * fixme 计算触摸点对应的字母:
     * todo 根据触摸点的y坐标除以cellHeight,得到的值就是字母对应的索引
     * todo  index = (int) getY()/cellHeight
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float y = event.getY();

                int index = (int) (y / cellHeight);

                // FIXME: 18/1/29 角标越界,可能是因为view高度多出了一块
                if (index<0 || index >indexArr.length-1) {
                    return true;
                }


                if (lastIndex != index) {
                    // FIXME: 2017/12/26 防止在一个字母区间多次调用 用一个变量记录上次的值做比较
                    // TODO: 2017/12/26 step1
                    Log.e("执行", "onTouchEvent: index---" + indexArr[index]);
                    if (listener != null) {
                        listener.onTouchLetter(indexArr[index]);
                    }
                }
                // TODO: 2017/12/26  step 2 赋值
                lastIndex = index;
                break;


            case MotionEvent.ACTION_UP:
                // TODO: 2017/12/26  step 3 up赋值
                lastIndex = -1;  // FIXME: 2017/12/26 记着
                break;


        }
        // TODO: 2017/12/26 重绘 点击和移动对应的字母变色,需要重绘
        invalidate(); //回调onDraw方法
        return true;
    }

    private int lastIndex = -1;//todo 记录上次的触摸字母索引


    public interface OnTouchLetterListener {
        void onTouchLetter(String word);
    }


}
